import sys

import pyautogui
import time

src =""

def setSrc():
    global  src
    strs = sys.argv[0].split("/")
    if(len(strs)==1):
        strs =strs[0].split("\\")
    fileName = strs[len(strs) - 1]

    src = sys.argv[0].replace(fileName,"")+"img/"

def click(img, sim=0.85, flag=0,timeout=10):
    timeout1= timeout
    img = src + img+".png"
    print(img)
    pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    x =0
    while (pos == None):

        pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
        timeout-=1
        print(timeout)
        time.sleep(0.1)
        if timeout ==timeout1/2:
            pyautogui.click()
            print("click")
        if  timeout<0:
            pyautogui.moveTo(1000,500)
            pyautogui.click()
            print("click2")
            return
    x = pos.x
    if flag!=0:
        x+=flag
    pyautogui.click(x,pos.y)
    time.sleep(0.5)
def find(img,sim=0.7):
    img = src + img + ".png"
    pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    while(pos ==None):
        pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    return  pos
def isExit(img,sim=0.8):
    time.sleep(0.5)
    img = src + img + ".png"
    pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    if pos ==None:
        return False
    return  True